<?php

class CategoryController extends Controller
{
	public function actionIndex($id)
	{
	   $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1;
      $limit = 10;
      $offset = $page*$limit - $limit;
      $criteria = new CDbCriteria();
      if($id > 0){
         $params = array();
         $criteria->addCondition('post_cat_id =:post_cat_id');
         $params[':post_cat_id'] = $id;
         $criteria->params = $params; 
	   }   
      $count = Post::model()->count($criteria);
      $order = 'post_date_update DESC';
      $post_order = 0;
      if(isset($_POST['Post'])){
         $post_order = $_POST['Post']['order'];         
         switch($post_order){
            case 0:
               $order = 'post_date_update DESC';
               break;
            case 1:
               $order = 'post_date_update DESC';
               break;
            case 2:
               $order = 'post_price ASC';
               break;
            case 3:
               $order = 'post_price DESC';
               break;
            case 4:
               $order = 'post_area ASC';
               break;
            case 5:
               $order = 'post_area DESC';
               break;
            default:
               $order = 'post_date_update DESC';
               break;
         }
         
      }
      $criteria->order = $order;
      $criteria->offset = $offset;
      $criteria->limit = $limit;
      $criteria->addCondition('post_active =1');
      $models = Post::model()->with('district','province')->findAll($criteria);
      $pagination = new Zebra_Pagination();
      $pagination->records($count);
      $pagination->records_per_page($limit);

      $this->render('index',array(
         'models' => $models,
         'pagination' => $pagination,
         'id' => $id,
         'post_order' => $post_order,
      ));
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}